<?php

namespace App\Admin\Actions\Category;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopCategoryModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;

class CategoryDeleteAction extends RowAction
{
    public $name = '删除';

    public function handle(ShopCategoryModel $model): Response
    {
        if (is_null($model) || $model->status_delete) return $this->response()->error(__('errorCode.433'))->refresh();

        $category = ShopCategoryModel::query()->where(['category_status' => 2, 'category_parent_id' => $model->category_id])->first();
        if (!is_null($category)) return $this->response()->error('检测到目标分类下存在未关闭的分类信息')->refresh();
        $model->status_delete = 1;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.category.delete');
        return $this->response()->success('操作成功')->refresh();
    }
}
